package com.agent.platform.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * Agent会话实体 - 对应Node.js版本的Session
 */
@Entity
@Table(name = "agent_sessions", indexes = {
    @Index(name = "idx_agent_id", columnList = "agent_id"),
    @Index(name = "idx_status", columnList = "status")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AgentSession {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private String id;

    @Column(name = "agent_id", nullable = false)
    private String agentId;

    @Column(name = "session_id", nullable = false, unique = true)
    private String sessionId;

    @Column(name = "status", length = 50)
    private String status = "idle"; // idle, running, stopped, failed, completed

    /**
     * 执行统计
     */
    @Column(name = "step_count")
    private Integer stepCount = 0;

    @Column(name = "failure_count")
    private Integer failureCount = 0;

    @Column(name = "total_tokens")
    private Integer totalTokens = 0;

    @Column(name = "total_cost")
    private Double totalCost = 0.0;

    /**
     * 会话元数据 - JSON格式
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "metadata", length = 4000)
    private Map<String, Object> metadata;

    /**
     * 最后一次错误信息
     */
    @Column(name = "last_error", length = 2000)
    private String lastError;

    @Column(name = "started_at")
    private LocalDateTime startedAt;

    @Column(name = "stopped_at")
    private LocalDateTime stoppedAt;

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    // 辅助方法
    public void incrementStepCount() {
        this.stepCount++;
    }

    public void incrementFailureCount() {
        this.failureCount++;
    }

    public void addTokens(int tokens) {
        this.totalTokens += tokens;
    }

    public void addCost(double cost) {
        this.totalCost += cost;
    }

    public boolean isRunning() {
        return "running".equals(status);
    }

    public void markAsRunning() {
        this.status = "running";
        if (this.startedAt == null) {
            this.startedAt = LocalDateTime.now();
        }
    }

    public void markAsStopped() {
        this.status = "stopped";
        this.stoppedAt = LocalDateTime.now();
    }

    public void markAsFailed(String error) {
        this.status = "failed";
        this.lastError = error;
        this.stoppedAt = LocalDateTime.now();
    }

    public void markAsCompleted() {
        this.status = "completed";
        this.stoppedAt = LocalDateTime.now();
    }
}
